package com.distribute.sso.service.impl;

import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import com.distribute.common.pojo.DistributeResult;
import com.distribute.common.util.CookieUtils;
import com.distribute.common.util.JsonUtils;
import com.distribute.mapper.DbAdministratorMapper;
import com.distribute.pojo.DbAdministrator;
import com.distribute.pojo.DbAdministratorExample;
import com.distribute.pojo.DbUser;
import com.distribute.pojo.DbAdministratorExample.Criteria;
import com.distribute.sso.dao.JedisClient;
import com.distribute.sso.service.AdministratorService;

@Service
public class AdministratorServiceImpl implements AdministratorService {

	@Autowired
	private JedisClient jedisClient;

	@Value("${REDIS_ADMINISTRATOR_SESSION_KEY}")
	private String REDIS_ADMINISTRATOR_SESSION_KEY;
	@Value("${SSO_SESSION_EXPIRE}")
	private Integer SSO_SESSION_EXPIRE;

	@Autowired
	DbAdministratorMapper dbAdministratorMapper;

	@Override
	public DistributeResult login(String account, String password, HttpServletRequest request,
			HttpServletResponse response) {
		DbAdministratorExample example = new DbAdministratorExample();
		Criteria criteria = example.createCriteria();
		criteria.andUsernameEqualTo(account);
		List<DbAdministrator> list = dbAdministratorMapper.selectByExample(example);
		// 如果没有此帐号
		if (null == list || list.size() == 0) {
			return DistributeResult.build(400, "帐号或密码错误");
		}
		DbAdministrator dbAdministrator = list.get(0);
		// 比对密码
		if (!DigestUtils.md5DigestAsHex(password.getBytes()).equals(dbAdministrator.getPassword())) {
			return DistributeResult.build(400, "帐号或密码错误");
		}
		// 生成token
		String token = UUID.randomUUID().toString();
		// 保存之前，把管理员对象中的密码清空。
		dbAdministrator.setPassword(null);
		// 把用户信息写入redis
		jedisClient.set(REDIS_ADMINISTRATOR_SESSION_KEY + ":" + token, JsonUtils.objectToJson(dbAdministrator));
		// 设置session的过期时间，到时间该key被删除
		jedisClient.expire(REDIS_ADMINISTRATOR_SESSION_KEY + ":" + token, SSO_SESSION_EXPIRE);

		// 添加写cookie的逻辑，cookie的有效期是关闭浏览器就失效。
		CookieUtils.setCookie(request, response, "ADMIN_TOKEN", token);
 		// 返回token
		return DistributeResult.ok(token);
	}
	
	@Override
	public DistributeResult getAdminstratorByToken(String token) {

		// 根据token从redis中查询管理员信息
		String json = jedisClient.get(REDIS_ADMINISTRATOR_SESSION_KEY + ":" + token);
		System.out.println(json+"jAAAAAAAAAAAAA");
		// 判断是否为空
		if (StringUtils.isBlank(json)) {
			return DistributeResult.build(400, "此session已经过期，请重新登录");
		}
		// 更新过期时间
		jedisClient.expire(REDIS_ADMINISTRATOR_SESSION_KEY + ":" + token, SSO_SESSION_EXPIRE);
		// 返回用户信息
		return DistributeResult.ok(JsonUtils.jsonToPojo(json, DbAdministrator.class));
	}

}
